package com.fs.service;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.controller.BaseController;
import com.fs.mapper.*;
import com.fs.pojo.Goods;
import com.fs.pojo.GoodsImages;
import com.fs.pojo.GoodsInfo;
import com.fs.vo.CommRequestVO;
import com.fs.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl extends BaseController implements GoodsSerivce {
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsFavouriteMapper goodsFavouriteMapper;

    @Autowired
    private GoodsImagesMapper goodsImagesMapper;

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Override
    public ResultVO list(CommRequestVO request) {
        if (request == null) return ResultVO.fail();

        int pageNum = request.getPageNum();
        int pageSize = request.getPageSize();

        if (pageNum < 1) pageNum = 1;
        if (pageSize < 1) pageNum = 10;

        Page<Goods> page = new Page(pageNum, pageSize);


        QueryWrapper<Goods> queryWrapper = new QueryWrapper();
        queryWrapper.like(StrUtil.isNotBlank(request.getQuery()), "username", request.getQuery())
                .orderByDesc("id");
        page = goodsMapper.selectPage(page, queryWrapper);
        request.setTotal(page.getTotal()).setRows(page.getRecords());

        return ResultVO.success(request);
    }

    @Override
    public ResultVO getGoodsList() {
        List<Map<String, Object>> goodsList = goodsMapper.getGoodsList();
        return ResultVO.success(goodsList);
    }



    @Override
    public ResultVO goodsInfo(Integer id) {
        Goods entity = goodsMapper.selectById(id);
        QueryWrapper<GoodsImages> wrapper=new QueryWrapper();
        wrapper.eq("goods_id",id);
        List<GoodsImages> imagesEntityList = goodsImagesMapper.selectList(wrapper);
        GoodsInfo goodsInfoEntity = goodsInfoMapper.selectById(id);
        entity.setGoodsInfoEntity(goodsInfoEntity)
                .setImagesList(imagesEntityList);
        return ResultVO.success(entity);
    }

}
